﻿using Microsoft.Office.Interop.Word;

namespace Songhay.Wpf.WordWalkingStick
{
    using Songhay.Composition;
    using Songhay.Office2010.Word;
    using Songhay.Wpf.WordWalkingStick.Views;

    /// <summary>
    /// Defines the MEF Composition Host for this application.
    /// </summary>
    /// <remarks>
    /// The class implementing <c>Office.IRibbonExtensibility</c>
    /// uses this Host to pass Ribbon interactions
    /// to the Composition Messaging for this application.
    /// </remarks>
    public sealed class WordWalkingStickCompositionHost : CompositionHost
    {
        /// <summary>
        /// Gets the MEF host.
        /// </summary>
        /// <value>The MEF host.</value>
        public static WordWalkingStickCompositionHost MefHost
        {
            get { return compositionHost; }
        }

        /// <summary>
        /// Composes the MEF Host.
        /// </summary>
        /// <param name="pathToWordCommandsDll">The path to word commands DLL.</param>
        /// <param name="pathToOpenXmlTransformDlls">The path to open XML transform DLLS.</param>
        public void Compose(string pathToWordCommandsDll, string pathToOpenXmlTransformDlls)
        {
            this.AddToAggregateCatalog(
                    CompositionUtility
                        .GetAssemblyCatalog(this.GetType()))
                .AddToAggregateCatalog(
                    CompositionUtility
                        .GetDirectoryOrAssemblyCatalog(pathToWordCommandsDll))
                .AddToAggregateCatalog(
                    CompositionUtility
                        .GetDirectoryOrAssemblyCatalog(pathToOpenXmlTransformDlls));

            this.WordCommands = new OfficeWordCommands();
            this.AddToCompositionBatch(this.WordCommands);
            this.AddToCompositionBatch(OpenXml.OpenXmlUtility.Transformations);

            base.Compose();

            if(this.CompositionMessaging == null)
                CompositionUtility
                    .ThrowCompositionException("The Composition Messaging object is null.");
            this.CompositionMessaging.RegisterMessageReceivers();

        }

        /// <summary>
        /// Gets or sets the Office Word commands.
        /// </summary>
        /// <value>The Word commands.</value>
        public OfficeWordCommands WordCommands { get; set; }

        /// <summary>
        /// Gets or sets the Office Word application.
        /// </summary>
        /// <value>The Word application.</value>
        /// <remarks>
        /// The original purpose for this member is
        /// to pass a reference of the Word Application
        /// to the class implementing <c>Office.IRibbonExtensibility</c>.
        /// </remarks>
        public Application WordApplication { get; set; }

        WordWalkingStickCompositionHost() { }

        static readonly WordWalkingStickCompositionHost compositionHost =
            new WordWalkingStickCompositionHost();
    }
}
